package com.gh.ssyx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.ssyx.common.result.Result;
import com.gh.ssyx.model.acl.Role;
import com.gh.ssyx.service.RoleService;
import com.gh.ssyx.vo.acl.AdminLoginVo;
import com.gh.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Eric Fang
 * @date 2023/6/18 14:43
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色分页列表
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("角色分页列表")
    public Result getPageList(RoleQueryVo roleQueryVo,
                              @PathVariable Long pageNum,
                              @PathVariable Long pageSize) {
        Page<Role> pageParam = new Page<>(pageNum,pageSize);
        IPage<Role> page = roleService.getPageList(roleQueryVo,pageParam);
        return Result.ok(page);
    }

    /**
     * 保存一个新角色
     */
    @PostMapping("/save")
    @ApiOperation("保存一个新角色")
    public Result saveRole(@RequestBody Role role) {
        boolean save = roleService.save(role);
        return save ? Result.ok(null) : Result.fail(null);
    }

    /**
     * 获取某个角色
     */
    @GetMapping("/get/{id}")
    @ApiOperation("获取某个角色")
    public Result getRoleById(@PathVariable Long id){
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 更新一个角色
     */
    @PutMapping("/update")
    @ApiOperation("更新一个角色")
    public Result updateRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        return b ? Result.ok(null) : Result.fail(null);
    }

    /**
     * 删除某个角色
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除某个角色")
    public Result removeById(@PathVariable Long id){
        boolean b = roleService.removeById(id);
        return b ? Result.ok(null) : Result.fail(null);
    }

    /**
     * 批量删除多个角色
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除多个角色")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean b = roleService.removeByIds(ids);
        return b ? Result.ok(null) : Result.fail(null);
    }

}
